<?php

/**
 * SiteController is the default controller to handle user requests.
 */
class SiteController extends CController {

    /**
     * Index action is the default action in a controller.
     */
    public function actionIndex()
    {

        $username = "Lưu";
        $email = 'luu.nguyen@voithan.com';

//        $this->render('index', array(
//            'usr' => $username,
//            'email' => $email
//        ));
        echo Yii::app()->createUrl('site/index', array('page' => 2));
        // Gọi đến file view của controller khác
//        $this->render('/home/index', array(
//            'usr' => $username,
//            'email' => $email
//        ));
    }

    public function actionNewPost()
    {
        $post = new Post();
        $post->title = 'sample post';
        $post->content = 'post body content';
        $post->category_id = 1;
        var_dump($post->save());
    }

    /*
     * Tham khao: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#reading-record
     */

    public function actionReadPost($id)
    {
        $post = Post::model()->findByPk($id);

        echo $post->title;
//        echo $post->id;
    }

    public function actionUpdatePost($id)
    {
        $post = Post::model()->findByPk($id);
        $post->title = 'new post title';
        $post->save(); // save the change to database        
    }

    public function actionDeletePost($id)
    {
        $post = Post::model()->findByPk($id); // assuming there is a post whose ID is $id
        $post->delete();                
    }

    public function actionTest()
    {
        echo "Hello Test";
    }

    public function demo()
    {
        echo "ok";
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error)
        {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

}
